package com.jboost.hyk.common.entity;


import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.jboost.common.constant.enums.ResultCode;
import com.jboost.common.exception.JBoostException;
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import com.jboost.common.system.base.entity.JBoostEntity;

import java.util.Calendar;

/**
 * @Description: 机构
 * @Author: cxn
 * @Date:   2022-10-09
 * @Version: V1.0
 */
@Data
@TableName("hyk_business")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="hyk_business对象", description="机构")
public class Business extends JBoostEntity{

    @TableLogic
    /**delFlag*/
    @ApiModelProperty(value = "delFlag")
    private Integer delFlag;

    /**入驻时间*/
    @ApiModelProperty(value = "入驻时间")
    private java.util.Date entryTime;

    /**有效期，前端输出的数字；*/
    @ApiModelProperty(value = "有效期，前端输出的数字；")
    private Integer validNum;

    /**有效期开始时间*/
    @ApiModelProperty(value = "有效期开始时间")
    private java.util.Date validStartTime;

    /**有效期结束时间*/
    @ApiModelProperty(value = "有效期结束时间")
    private java.util.Date validEndTime;

    /**名称*/
    @ApiModelProperty(value = "名称")
    private String name;

    /**编号*/
    @ApiModelProperty(value = "编号")
    private String code;

    /**机构类型：personal-个人；corporate-企业；*/
    @ApiModelProperty(value = "机构类型：personal-个人；corporate-企业；")
    private String type;

    /**状态：new-待激活；audit-待审核；valid-已 启用；invalid-已停用；expired-已过期；*/
    @ApiModelProperty(value = "状态：new-待激活；audit-待审核；valid-已 启用；invalid-已停用；expired-已过期；")
    private String status;

    /**logo文件id*/
    @ApiModelProperty(value = "logo文件id")
    private String logoFileId;

    /**机构账号手机，冗余保存*/
    @ApiModelProperty(value = "机构账号手机，冗余保存")
    private String phone;

    /**tagIds*/
    @ApiModelProperty(value = "tagIds")
    private String tagIds;

    @ApiModelProperty(value = "客户管理员id")
    private String managerId;

    @ApiModelProperty(value = "可分配账号总数")
    private Integer accountTotal;
    @ApiModelProperty(value = "已使用账号数量")
    private Integer accountUsed;
    @ApiModelProperty(value = "知识点余额")
    private Integer pointBalance;

    @ApiModelProperty(value = "待处理的专业数量")
    private Integer paddingMajorCount;



    /**
     * 使用账号
     * @param num
     */
    public void useAccount(int num) {
        if (this.accountTotal == null || this.accountTotal == 0){
            throw new JBoostException(ResultCode.BUSINESS_ACCOUNT_TOTAL_ZERO);
        }
        int total = this.accountTotal - this.accountUsed - num;
        if(total < 0){
            throw new JBoostException(ResultCode.BUSINESS_ACCOUNT_TOTAL_LOW);
        }
        this.accountUsed+=num;
    }
    public void freedAccount(int num) {
        int freed = this.accountUsed-num;
        this.accountUsed = freed<0?0:freed;
    }
    public Integer accountBalance() {
        return this.accountTotal - this.accountUsed;
    }

    public Integer validMonth(){
        //根据结束时间判断还剩几个月
        Calendar endCal = Calendar.getInstance();
        Calendar nowCal = Calendar.getInstance();
        endCal.setTime(this.getValidEndTime());
        if (endCal.getTime().before(nowCal.getTime())){
            return 0;
        }
        int yearEnd = endCal.get(Calendar.YEAR);
        int yearNow = nowCal.get(Calendar.YEAR);
        int month1 = endCal.get(Calendar.MONTH);
        int month2 = nowCal.get(Calendar.MONTH);
        // 获取年的差值
        int yearInterval = yearEnd - yearNow;
        // 获取月数差值
        int monthInterval = month1 - month2;
        int monthsDiff = yearInterval * 12 + monthInterval;
        return monthsDiff;
    }

}
